package com.example.collegevice.controller;

import com.example.collegevice.service.AdminService;
import com.example.collegevice.service.StudentService;
import com.example.collegevice.service.TeacherService;
import com.example.common.Result;
import com.example.pojo.Account;
import com.example.utils.JwtUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
public class WebController {

    @Resource
    private AdminService adminService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;
    /**
     * 默认请求接口
     */
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        if ("ADMIN".equals(account.getRole())) {
            Account admin = adminService.login(account);
            String token = JwtUtils.createToken(admin.getId().toString(), admin.getName());
//            System.out.println("login token is "+token);
            return Result.success(Map.of("token", token, "data", admin));
        }
        if ("TEACHER".equals(account.getRole())) {
            Account teacher = teacherService.login(account);
            String token = JwtUtils.createToken(teacher.getId().toString(), teacher.getName());
            return Result.success(Map.of("token", token, "data", teacher));
        }
        if ("STUDENT".equals(account.getRole())) {
            Account student = studentService.login(account);
            String token = JwtUtils.createToken(student.getId().toString(), student.getName());
            return Result.success(Map.of("token", token, "data", student));
        }
        return Result.success();
    }


    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if ("ADMIN".equals(account.getRole())) {
            adminService.updatePassword(account);
        }
        if ("TEACHER".equals(account.getRole())) {
            teacherService.updatePassword(account);
        }
        if ("STUDENT".equals(account.getRole())) {
            studentService.updatePassword(account);
        }
        return Result.success();
    }

}
